<template>
  <div class="container">
    <ul>
      <el-card shadow="always">
        <li>
          <div class="title">
            <span class="line"></span>
            <span class="title-text">学校模块设置</span>
            <span class="line"></span>
          </div>
          <div class="content">
            <div>
              <span>配置名称：&nbsp;&nbsp;{{from.configName}}</span>
            </div>
            <div>
              <span>是否启用:</span>  <el-switch @change="changeEnable" v-model="from.enable" />
            </div>
            <div v-show="isShow">
              <span>是否允许教师有协助管理课程的权利:</span>  <el-switch @change="changeAllowAssistManageCourse" v-model="from.allowAssistManageCourse" />
            </div>
          </div>
        </li>
        <div class="bottom">
          <el-button type="primary" @click="changeSysConfig">修改</el-button>
        </div>
      </el-card>
    </ul>
  </div>
</template>

<script lang='ts' setup>
import {
  defineExpose, reactive, getCurrentInstance, ref, onMounted,
} from 'vue';
import Request from '@/api/request/request';
import { ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance();
const isShow = ref(false);
const from = ref({
  id: 0,
  configName: '',
  enable: false,
  allowAssistManageCourse: false,
});

const changeEnable = (val:boolean) => {
  from.value.enable = val;
};

const changeAllowAssistManageCourse = (val:boolean) => {
  from.value.allowAssistManageCourse = val;
};

const getConfig = async () => {
  const resData:any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_AUTHORITY_SCHOOLCONFIG}/all`).get();
  if (resData.code === 200) {
    console.log(`学校模块设置${JSON.stringify(resData.data)}`);
    from.value.id = resData.data[0].id;
    from.value.configName = resData.data[0].name;
    from.value.enable = resData.data[0].enable;
    if (from.value.enable) {
      proxy.isShow = true;
    } else {
      proxy.isShow = false;
    }
    from.value.allowAssistManageCourse = resData.data[0].allowAssistManageCourse;
  }
};
onMounted(() => {
  getConfig();
});

const changeSysConfig = async () => {
  const resData:any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_AUTHORITY_SCHOOLCONFIG}`, from.value).put();
  if (resData.code === 200) {
    ElMessage.success('修改成功');
    getConfig();
  }
};
</script>

<style lang="scss" scoped>
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  .container {
    ul {
      list-style: none;
      li {
        width: 100%;
        height: auto;
        display: flex;
        flex-direction: column;
        .title {
          display: flex;
          justify-content: center;
          align-items: center;
          text-align: center;
          span:nth-child(1) {
            flex: 1;
          }
          span:nth-child(3) {
            flex: 1;
          }
          .line {
            display: inline-block;
            width: 50%;
            max-width: 50%;
            border-bottom: 1px solid rgb(214,222,228);
          }
        }
        .content {
          width: 100%;
          padding: 1% 10%;
          display: flex;
          flex-direction: column;
          div {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            span {
              display: inline-block;
              height: 32px;
              margin-right: 20px;
            }
          }
        }
      }
    }
    .bottom {
      width: 100%;
      display: flex;
      justify-content: center;
      .el-button {
        width: 50%;
        text-align: center;
      }
    }
  }
</style>
